<?php
/**
 * <b>RUBIK IT Solution & Development ltd.</b>
 *
 * PROJECT		: Rubik Ultimate Framework
 * @version 	: 1.1
 * COPYRIGHT    : 2008
 * ------------------------------------------------------
 * 
 * Created on	:  Jun 25, 2008
 * 
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls("html.AbstractHtmlUploadForm" );

/**
 * Class for creating the ajax upload file control
 *
 */
class UploadControl extends AbstractHtmlUploadForm {
	
	/**
	 * Create the HTML text for doing upload
	 *
	 * @param int $inputSize view size of html
	 * @param string $cssCls
	 * @return string: html string
	 */
	function createUploadFormHtml($inputSize) {
		static $id;
		if (empty ( $id )) {
			$id = 1;
			// print the check javascript for the first time
			$script = '
			function checkUploadTypeForm (inputId, inputName) {
				var icheck = document.getElementById(inputId+"_status");
				var iurl = document.getElementById(inputId+"_link");
				var ifile = document.getElementById(inputId+"_file");
				var ivalidate = document.getElementById(inputName+"_ok");
				ivalidate.value = \'empty\';
				if (icheck.checked == true) {
					iurl.style.display = \'none\';
					ifile.style.display = \'block\';
				} else {
					iurl.style.display = \'block\';
					ifile.style.display = \'none\';
				}
			}
			
			function checkValidFileTypeFroUpload(InputId, inputType, ele , acceptFileType) {
				if (acceptFileType == \'\' || acceptFileType == undefined) {
					return true;
				} 
				var ichange = document.getElementById(InputId+"_change");
				var ivalidate = document.getElementById(InputId+"_ok");
				ivalidate.value = \'no\';	
				if (inputType == "text") {
					ichange.value = \'url\';
				} else if (inputType == "file") {
					ichange.value = \'file\';
				}
			
				testString = ele.value;
				testString = testString.toLowerCase();;
				
				arrType = acceptFileType.toLowerCase().split(",");
				
				size = arrType.length;
				for (i = 0; i < size; i++) {
					ext = trim(arrType[i]);
					if (ext != "" && testString.lastIndexOf(ext) != -1){
						ivalidate.value = \'yes\';
						return true;
					}
				}
			
				if (inputType == "text") {
					alert("Your input url type should in: " + acceptFileType);
				} else if (inputType == "file") {
					alert("Your input file type should in: " + acceptFileType);
				}
				emptyValue(ele);
				ivalidate.value = \'no\';
				ele.focus();
				return false;
			}
			';
			SysResponse::addJavascriptContent ( $script );
		}
		$inputName = $this->_inputName;
		$supportFileTypes = $this->_supportedTypes;
		$value = $this->_value;
		
		$strCommentTypeUpload = "";
		$strCommentTypeUpload = "<em>(Only accept file type: $supportFileTypes) </em>";
		$inputId = $inputName . $id;
		
		if (! strpos ( $value, '://' ) && $value != '') {
			$value = $this->getRootUrlPath () . $value;
		}
		if (empty ( $this->_value )) {
			$ivalidatevalue = 'no';
		} else {
			$ivalidatevalue = 'yes';
		}
		$sout = '<div  style="float: left; width:15px;"><input style="width:15px;" type="checkbox" name="' . $inputName . '_status" id="' . $inputId . '_status" value="1" onclick="checkUploadTypeForm(\'' . $inputId . '\',\'' . $inputName . '\');"> </div>';
		$sout .= '<div id="' . $inputId . '_link"><input name="' . $inputName . '" id="' . $inputName . '_link" type="text" value="' . $value . '" size="' . $inputSize . '" onchange="checkValidFileTypeFroUpload(\'' . $inputName . '\',\'text\',this,\'' . $supportFileTypes . '\');" /></div>';
		$sout .= '<div id="' . $inputId . '_file" style="display:none"><input name="' . $inputName . '" id="' . $inputName . '_file" type="file" onchange="checkValidFileTypeFroUpload(\'' . $inputName . '\',\'file\',this,\'' . $supportFileTypes . '\');" size="' . $inputSize . '" /></div>';
		$sout .= '<input type="hidden" name="' . $inputName . '_change" id="' . $inputName . '_change" value="nochange" />';
		$sout .= '<input type="hidden" name="' . $inputName . '_old_value" id="' . $inputName . '_old_value" value="' . $this->_value . '" />';
		$sout .= '<input type="hidden" name="' . $inputName . '_ok" id="' . $inputName . '_ok" value="' . $ivalidatevalue . '" />';
		$sout .= $strCommentTypeUpload;
		$id ++;
		return $sout;
	}

}
?>